package com.duoqin.syncassistant.ui;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.BroadcastReceiver;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.ListPopupWindow;
import android.view.View;
import android.widget.TextView;

import com.duoqin.common.ui.AbsActivity;
import com.duoqin.syncassistant.R;

import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class BaseSyncActivity extends AbsActivity {

    protected ExecutorService mExecutorService = Executors.newSingleThreadExecutor();

    protected View mDimBgView, mBackupView, mRestoreView, mProgressView;
    protected TextView mPhoneRecordsCountView, mServerRecordsCountView;
    protected ListPopupWindow mListPopupWindow;
    protected ArrayList<String> mRecordIds = new ArrayList<>();
    protected int mPhoneRecordsCount = 0;
    protected BroadcastReceiver mBroadcastReceiver;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_records_sync);

        mDimBgView = findViewById(R.id.dim_background);
        mProgressView = findViewById(R.id.sync_progress);
        mBackupView = findViewById(R.id.backup_records);
        mBackupView.setOnClickListener(this);
        mRestoreView = findViewById(R.id.restore_records);
        mRestoreView.setOnClickListener(this);
        mPhoneRecordsCountView = findViewById(R.id.phone_count);
        mPhoneRecordsCountView.setText(String.valueOf(mPhoneRecordsCount));
        mServerRecordsCountView = findViewById(R.id.server_count);

        setupModelObserver();
    }

    protected void queryLocalRecords() {
    }

    protected void setupModelObserver() {
    }

    @Override
    protected void onResume() {
        super.onResume();
        queryLocalRecords();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mListPopupWindow != null && mListPopupWindow.isShowing()) {
            mListPopupWindow.dismiss();
        }
    }

    @Override
    public void onClick(View v) {
        if (R.id.backup_records == v.getId()) {
            attemptBackup();
        } else if (R.id.restore_records == v.getId()) {
            attemptRestore();
        } else {
            super.onClick(v);
        }
    }

    protected void attemptRestore() {
    }

    protected void attemptBackup() {
    }

    /**
     * Shows the progress UI and hides the login form.
     */
    protected void showProgress(final boolean show) {
        showProgress(show, null);
    }
    protected void showProgress(final boolean show, final View focusView) {
        if (show) {
            mBackupView.setVisibility(show ? View.GONE : View.VISIBLE);
            mRestoreView.setVisibility(show ? View.GONE : View.VISIBLE);
        }

        int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime);
        mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
        mProgressView.animate().setDuration(shortAnimTime).alpha(
                show ? 1 : 0).setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
                mBackupView.setVisibility(show ? View.GONE : View.VISIBLE);
                mRestoreView.setVisibility(show ? View.GONE : View.VISIBLE);
                if (focusView != null) {
                    focusView.requestFocus();
                }
            }
        });
    }
}
